﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Model;
using GPI.Server;
using Server;
using Moon.Common;
using Moon.Common.WebInfo;
using System.IO;


public partial class Admin_DictionaryManage_ImageAttributeManage : System.Web.UI.UserControl
{
    private readonly DictionaryService server = new DictionaryService();
    /// <summary>
    /// 加载页面
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string nodeid = this.Request.QueryString["id"] != null ? this.Request.QueryString["id"].ToString() : "";
            this.lbnAddImageAtt.CommandArgument = nodeid;
            //讲所属类别的ID赋值给添加类别按钮
            BindTable(nodeid);

        }
    }
    #region 编辑图片属性
    /// <summary>
    /// 进入信息条编辑
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lbnAddImageAtt_Click(object sender, EventArgs e)
    {
        this.MultiView1.ActiveViewIndex = 1;
    }

    /// <summary>
    /// 保存信息
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSaveClass_Click(object sender, EventArgs e)
    {
        //判断保存的是什么类型的数据
        DictionaryInfo model = new DictionaryInfo();

        model.id = this.btnSaveClass.CommandArgument.ToInt(0);
        model.Nodeid = this.lbnAddImageAtt.CommandArgument.ToInt(0);
        model.Name = this.txtClassName.Text;

        model.Type = this.ddlType.SelectedValue;
        model.ParentID = 0;

        //如果存在封面
        if (fuImagePath.HasFile)
        {
            CoFileHepler.DeleteFile(this.Application["WebPath"] + this.labImagePath.Text);
            model.ImagePath = this.fuImagePath.FileName.GetFileExt().IsNullOrEmpty() ? "" : WebConfigInfo.ImgPath + Guid.NewGuid().ToString() + this.fuImagePath.FileName.GetFileExt();
            upFile(fuImagePath, model.ImagePath, this.Application["WebPath"].ToString(), true, WebConfigInfo.Imgformat);
        }
        else
        {
            model.ImagePath = labImagePath.Text;
        }
        //如果存在附件
        if (this.fuFile.Visible && this.fuFile.HasFile)
        {
            CoFileHepler.DeleteFile(this.Application["WebPath"] + this.txtValues.Text);
            model.Values = this.fuFile.FileName.GetFileExt().IsNullOrEmpty() ? "" : WebConfigInfo.FilePath + Guid.NewGuid().ToString() + this.fuFile.FileName.GetFileExt();
            upFile(fuFile, model.Values, this.Application["WebPath"].ToString(), true, WebConfigInfo.Fileformat);
        }
        else
        { model.Values = this.txtValues.Text; }
        bool isAdd = false;
        if ("0".Equals(model.id.ToString()))
        {
            isAdd = server.Add(model);
        }
        else
        {
            isAdd = server.Update(model);
        }
        if (isAdd)
        {
            MessageBox.Show(this.Page, "操作成功！");
        }
        else
        {
            MessageBox.Show(this.Page, "操作失敗！");
        }
        //讲所属类别的ID赋值给添加类别按钮
        BindTable(this.lbnAddImageAtt.CommandArgument);
    }
    /// <summary>
    /// 列表方法
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    protected void repDictionaryList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int id = 0;
        DictionaryInfo model = new DictionaryInfo();
        switch (e.CommandName)
        {

            case "edit":
                this.MultiView1.ActiveViewIndex = 1;
                this.btnSaveClass.CommandName = "";
                id = e.CommandArgument.ObjectToInt(0);

                model = server.GetModel(id);
                this.txtClassName.Text = model.Name;
                this.txtValues.Text = model.Values;
                this.btnSaveClass.CommandArgument = model.id.ToString();
                this.ddlType.SelectedValue = model.Type;
                this.labImagePath.Text = model.ImagePath;
                seleteTag();
                break;
            case "delete":
                id = e.CommandArgument.ObjectToInt(0);
                model = server.GetModel(id);
                CoFileHepler.DeleteFile(this.Application["WebPath"].ToString() + model.ImagePath);
                if ("file".Equals(model.Type))
                {
                    CoFileHepler.DeleteFile(this.Application["WebPath"].ToString() + model.Values);
                }
                if (server.Delete(id))
                {
                    MessageBox.Show(this.Page, "操作成功！");
                }
                else
                {
                    MessageBox.Show(this.Page, "操作失敗！");
                }
                BindTable(model.ParentID.Value.ToString());
                break;

        }
    }
    /// <summary>
    /// 绑定右侧列表
    /// </summary>
    /// <param name="type"></param>
    /// <param name="id"></param>

    private void BindTable(string id)
    {
        DataSet ds = null;
        ds = server.GetList(" Nodeid=" + id);
        if (ds != null)
        {
            this.repDictionaryList.DataSource = ds;
            this.repDictionaryList.DataBind();
        }
        this.lbnAddImageAtt.CommandName = "";
        this.lbnAddImageAtt.CommandArgument = id;
        this.MultiView1.ActiveViewIndex = 0;

        this.MultiView1.Visible = true;

    }
    /// <summary>
    /// 绑定左侧列表
    /// </summary>
    /// <param name="nodeid"></param>
    private void BindList(string nodeid)
    {
        Clear();
        //新建树，用于生成左边列表栏
        TreeNode tn = new TreeNode();
        NodeInfo model = new NodeService().GetModel(nodeid.ToInt(0));
        if (model != null)
        {
            tn.Text = model.NodeName;
            tn.Value = model.Id.ToString();
            tn.Target = "Node";
            tn.Selected = true;
            //递归获取子树，从而生成子节点
            foreach (TreeNode chNode in server.GetAllNode(model.Id))
            {
                tn.ChildNodes.Add(chNode);
            }
        }
    }
    /// <summary>
    /// 清理方法
    /// </summary>
    private void Clear()
    {
        this.txtClassName.Text = "";
        this.txtValues.Text = "";
        this.MultiView1.ActiveViewIndex = 0;
    }
    /// <summary>
    /// 选择信息类别
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
    {

        seleteTag();
    }
    /// <summary>
    /// 变化类型
    /// </summary>
    private void seleteTag()
    {
        switch (this.ddlType.SelectedValue)
        {
            case "http":
                this.txtValues.Visible = true;
                this.fuFile.Visible = false;
                break;
            case "file":
                this.fuFile.Visible = true;
                this.txtValues.Visible = false;
                break;
            default:
                this.txtValues.Visible = true;
                this.fuFile.Visible = false;
                break;
        }
    }

    protected void lbnReturnList_Click(object sender, EventArgs e)
    {
        //讲所属类别的ID赋值给添加类别按钮
        BindTable(this.lbnAddImageAtt.CommandArgument);
    }
    #endregion


    #region 编辑节点属性
    /// <summary>
    /// 編輯封面
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lbnEditPage_Click(object sender, EventArgs e)
    {
        this.MultiView1.ActiveViewIndex = 2;
        NodeInfo model = new NodeService().GetModel(this.lbnAddImageAtt.CommandArgument.ToInt(0));
        this.Image1.ImageUrl = "../../" + model.NodeImage;
        this.hfImage.Value =model.NodeImage;
        this.btnSaveTemplate.CommandArgument = model.Id.ToString();
    }
    /// <summary>
    /// 保存更新信息
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSaveTemplate_Click(object sender, EventArgs e)
    {
        NodeInfo model = new NodeService().GetModel(this.btnSaveTemplate.CommandArgument.ToInt(0));
        //如果存在封面
        if (fuImageTemplate.HasFile)
        {
            CoFileHepler.DeleteFile(this.Application["WebPath"] + this.hfImage.Value);
            model.NodeImage = this.fuImageTemplate.FileName.GetFileExt().IsNullOrEmpty() ? "" : WebConfigInfo.ImgPath + Guid.NewGuid().ToString() + this.fuImageTemplate.FileName.GetFileExt();
            upFile(fuImageTemplate, model.NodeImage, this.Application["WebPath"].ToString(), true, WebConfigInfo.Imgformat);
        }
        else
        {
            model.NodeImage = this.hfImage.Value;
        }
        if (new NodeService().Update(model))
        {
            MessageBox.Show(this.Page, "操作成功！");
        }
        else
        {
            MessageBox.Show(this.Page, "操作失敗！");
        }
        BindTable(model.Id.ToString());
    }
    #endregion


    /// <summary>
    /// 上傳操作
    /// </summary>
    /// <param name="Fupload">上傳空間</param>
    /// <param name="SaveName">保存名</param>
    /// <param name="dir">保存目錄</param>
    /// <param name="CheckType">驗證類型字符串</param>
    /// <returns></returns>
    private bool upFile(System.Web.UI.WebControls.FileUpload Fupload, string SaveName, string dir, bool isNULL, string CheckType)
    {
        //判断上传文件是否是空
        if (!Fupload.HasFile && isNULL)
        {
            return true;
        }
        //判断目录是否存在不存在则创建
        if (!Directory.Exists(dir))
        {
            Directory.CreateDirectory(dir);
        }
        //删除久的文件
        if (new FileInfo(dir + SaveName).Exists)
        {
            CoFileHepler.DeleteFile(dir + SaveName);
        }
        try
        {
            //獲得文件類型
            string type = CoFileHepler.GetPostfixStr(Fupload.FileName);
            //判斷文件類型是否為需要的類型
            if (CoFileHepler.CheckValidExt(type, CheckType))
            {
                Fupload.PostedFile.SaveAs(dir + SaveName);
            }
            return true;
        }
        catch
        {
            return false;
        }
    }



}
